package com.anjho.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "distributor")
@NamedQueries(value = {
		@NamedQuery(name = "Distributor.getDistributorList", query = "SELECT d FROM Distributor d"),
		@NamedQuery(name = "Distributor.getDistributorByDicode", query = "SELECT d FROM Distributor d WHERE d.dicode = :dicode") })
public class Distributor implements Serializable {

	private static final long serialVersionUID = -5032574662192861153L;

	public Distributor() {
		this.setDicode(0);
		this.setPerson(new Person());
	}

	@Id
	@Column(name  = "dicode")
	@OneToMany(mappedBy = "distributor")
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private int dicode;

	@OneToOne(cascade = {CascadeType.PERSIST})
	@JoinColumn(name = "document", nullable = false)
	private Person person;

	@Column(name = "segment", length = 50)
	private String segment;

	public String getSegment() {
		return segment;
	}

	public void setSegment(String segment) {
		this.segment = segment;
	}

	public int getDicode() {
		return dicode;
	}

	public void setDicode(int dicode) {
		this.dicode = dicode;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getFormattedDocument() {
		return this.person.getDocument().substring(0, 2) + "."
				+ this.person.getDocument().substring(2, 5) + "."
				+ this.person.getDocument().substring(5, 8) + "/"
				+ this.person.getDocument().substring(8, 11) + "-"
				+ this.person.getDocument().substring(11, 12);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + dicode;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Distributor other = (Distributor) obj;
		if (dicode != other.dicode)
			return false;
		return true;
	}

}
